<template>
  <Center>
    <form action class="login-container" @submit.prevent="handleSubmit">
      <div class="form-item">
        <div class="input">
          <label for="text">账号：</label>
          <input id="text" @input="validateEmpty('loginId','账号不能为空')"  type="text" v-model="userInfo.loginId" />
        </div>
        <FormError :msg="error.loginId" />
      </div>
      <div class="form-item">
        <div class="input">
          <label for="pwd">密码：</label>
          <input id="pwd" v-model="userInfo.loginPwd"  @input="validateEmpty('loginPwd','密码不能为空')" type="password" autocomplete="new-password" />
        </div>
        <FormError :msg="error.loginPwd" />
      </div>
      <div class="form-item">
        <div class="input">
          <label for="pwd1">重复密码：</label>
          <input id="pwd1" @input="validatePwdAgain" v-model="userInfo.pwdAgain" type="password" autocomplete="new-password" />
        </div>
        <FormError :msg="error.pwdAgain" />
      </div>
      <div class="form-item">
        <div class="input">
          <label for="t1">昵称：</label>
          <input id="t1" v-model="userInfo.nickname" @input="validateEmpty('nickname','请输入昵称')" type="text" autocomplete="new-password" />
        </div>
        <FormError :msg="error.nickname" />
      </div>
      <div class="form-item">
        <div class="input">
          <label></label>
          <button>注册</button>
        </div>
        <FormError :isCenter="true"  />
      </div>
    </form>
  </Center>
</template>

<script>
import {reg} from "../services/userService"
import Center from "../components/Center";
import FormError from "../components/FormError";
export default {
  components: {
    Center,
    FormError,
  },
  data() {
    return {
      userInfo: {
        loginId: "",
        loginPwd: "",
        pwdAgain: "",
        nickname: "",
      },
      error: {
        loginId: "",
        loginPwd: "",
        pwdAgain: "",
        nickname: "",
      },
    };
  },
  methods: {
    //非空验证
    validateEmpty(field,msg) {
        if(this.userInfo[field]){
            this.error[field] = ""
            return true
        }else{
            this.error[field] = msg
            return false
        }
    },
   async handleSubmit(){
        let validate1 = this.validateEmpty('loginId','账号不能为空')
        let validate2 = this.validateEmpty('loginPwd','密码不能为空')
        let validate3 = this.validatePwdAgain()
        let validate4 = this.validateEmpty('nickname','请输入昵称')
        if(validate1 & validate2 & validate3 & validate4){
            // 提交数据
            var resp = await reg(this.userInfo)
            if(resp.data){
                // 注册成功
                alert("注册成功！！！")
                this.$router.push({
                    name:"Login"
                })
            }else{
                this.error.loginId = resp.msg;
            }
        }
    },
    validatePwdAgain(){
        var result = this.validateEmpty("pwdAgain","重复密码不能为空")
        if(result){
            if(this.userInfo.loginPwd !== this.userInfo.pwdAgain){
                this.error.pwdAgain = "两次密码不一致"
                return false
            }
        }
        return true
    }
  },
};
</script>

<style scoped>
.login-container {
  width: 400px;
}
.form-item .input {
  display: flex;
  outline: none;
}
.form-item label {
  width: 100px;
  display: inline-block;
  text-align: right;
  line-height: 41px;
}

.form-item input,
.form-item button {
  flex-grow: 1;
}
</style>